﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NeuralStock.Entities;
using System.IO;
using NeuralStock.Common;
using System.Globalization;
using System.Text.RegularExpressions;

namespace NeuralStock.Tools
{
    public class MoneyCsvReader
    {
        public List<FinancialReportData> GetData()
        {
            List<FinancialReportData> result = new List<FinancialReportData>();

            var files = Directory.GetFiles(DirectoryRepository.MoneyDataDirectory);

            foreach (var file in files)
            {
                var reports = GetSingleFileData(file);
                result.AddRange(reports);
            }

            return result;
        }

        private List<FinancialReportData> GetSingleFileData(string path)
        {
            List<FinancialReportData> result = new List<FinancialReportData>();

            string companyName = Path.GetFileName(path).Replace(".csv", "");

            var lines = File.ReadAllLines(path).Select(a => a.Split(';')).ToList();

            int columnLimit = lines.First().Count();

            for (int i = 0; i < columnLimit; i++)
            {
                FinancialReportData newReport = new FinancialReportData();
                var date = DateTime.Parse(lines.First()[i]);
                if (date.Month == 12)
                {
                    newReport.Year = date.Year;
                }
                else
                {
                    newReport.Year = date.Year - 1;
                }
                
                newReport.CompanyName = companyName.ToUpper();
                result.Add(newReport);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NetRevenuesFromSalesOfGoodsAndMaterials = ParseDecimal(lines.ElementAt(1)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).ProfitFromOperatingActivities = ParseDecimal(lines.ElementAt(2)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NetProfit = ParseDecimal(lines.ElementAt(4)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NetCashFlowsFromOperatingActivities = ParseDecimal(lines.ElementAt(6)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NetCashFlowsFromInvestmentActivities = ParseDecimal(lines.ElementAt(7)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NetCashFlowsFromFinancialActivities = ParseDecimal(lines.ElementAt(8)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).Assets = ParseDecimal(lines.ElementAt(9)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).LiabilitiesAndProvisionsForLiabilities = ParseDecimal(lines.ElementAt(10)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).LongTermLiabilities = ParseDecimal(lines.ElementAt(11)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).ShortTermLiabilities = ParseDecimal(lines.ElementAt(12)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).EquityCapital = ParseDecimal(lines.ElementAt(13)[i]);
            }

            for (int i = 0; i < columnLimit; i++)
            {
                result.ElementAt(i).NumberOfShares = ParseDecimal(lines.ElementAt(15)[i]);
            }

            return result;
        }

        private decimal? ParseDecimal(string input)
        {
            input = input.Replace(" ", "");
            input = Regex.Replace(input, @"[^\u0000-\u007F]", string.Empty);

            if (input.Equals("-"))
            {
                return null;
            }

            return decimal.Parse(input);
        }
    }
}
